/**
 * 
 */
package vues;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.HeadlessException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import modele.Bot;
import modele.BotForet;
import modele.Feu;
import modele.MapEntite;
import modele.MapRobot;
import modele.Simulation;
import evenements.EcouteurBarMenu;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author S219
 * @generated "UML vers Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class IHM extends JFrame{
	
	private JPanel pan;
	
	public JPanel getPan() {
		return pan;
	}

	private JMenuBar menubar;
	private JMenu simuMenu, robotMenu, feuMenu;
	private JMenuItem start,stop,quitter;
	
	public IHM(Simulation s){
		this.simulation = s;
		
		this.setTitle("FireBot");
		this.setMinimumSize(new Dimension(600, 300));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1150, 900);
		this.setLocationRelativeTo(null);
		this.pan = new JPanel();
		GridLayout layout = new GridLayout(10,10);
		this.pan.setLayout(layout);
		this.initGrille();
		this.initMenuBar();
		this.setContentPane(pan);
		this.setVisible(true);
	}
	
	public void initGrille(){
		this.pan.removeAll();
		for (int i = 0; i < 100; i++){
			JButton button = new JButton(/*i%10+", "+i/10*/);
			Color light_green = new Color(149, 219, 123);
			button.setBackground(light_green);
			for(MapEntite m : this.getSimulation().getMapEntites())
			{
				if(m.getCaseCourante().getX() == i%10 && m.getCaseCourante().getY() == i/10)
				{
					if(m instanceof Feu){	
						ImageIcon imgFeu = new ImageIcon((new ImageIcon(getClass().getResource("fire.png")).getImage()).getScaledInstance(70, 70, java.awt.Image.SCALE_SMOOTH));
						button.setIcon(imgFeu);
					}
					else if (m instanceof Bot){
						ImageIcon imgRobot = new ImageIcon((new ImageIcon(getClass().getResource("robot-"+((Bot)m).getOrientationImage()+".png")).getImage()).getScaledInstance(70, 70, java.awt.Image.SCALE_SMOOTH));
						button.setIcon(imgRobot);
					}
				}
			}			
			this.pan.add(button);

		}
	}
	
	private void initMenuBar() {
		EcouteurBarMenu ecouteurMenu = new EcouteurBarMenu(this);
		menubar = new JMenuBar();
		simuMenu = new JMenu("Simulation");
		robotMenu = new JMenu("Robots");
		feuMenu = new JMenu("Feux");
		start = new JMenuItem("Lancer la simulation");
		start.addActionListener(ecouteurMenu);
		stop = new JMenuItem("Stopper la simulation");
		stop.addActionListener(ecouteurMenu);
		quitter = new JMenuItem("Quitter");
		quitter.addActionListener(ecouteurMenu);
		this.simuMenu.add(start);
		this.simuMenu.add(stop);
		this.simuMenu.addSeparator();
		this.simuMenu.add(quitter);
		this.menubar.add(simuMenu);
		this.menubar.add(robotMenu);
		this.menubar.add(feuMenu);
		this.setJMenuBar(menubar);
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @generated "UML vers Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private Simulation simulation;

	public Simulation getSimulation() {
		return simulation;
	}

	public void setSimulation(Simulation simulation) {
		this.simulation = simulation;
	}
	
	public void refresh(){
		initGrille();
		this.revalidate();

		
	}
}